package internact;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 管理员模块
 */

public class AdminPanel extends JPanel {
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton displayAllMembersButton;
    private JButton displayMemberInfoButton;
    private JButton displayMemberLogoutRecordsButton;
    private JButton addMemberButton;
    private JButton deleteMemberButton;
    private AdminManager adminManager;
    private MemberManager memberManager;

    public AdminPanel(AdminManager adminManager, MemberManager memberManager) {
        this.adminManager = adminManager;
        this.memberManager = memberManager;

        setLayout(new GridLayout(6, 2));

        usernameLabel = new JLabel("用户名：");
        add(usernameLabel);
        usernameField = new JTextField();
        add(usernameField);

        passwordLabel = new JLabel("密码：");
        add(passwordLabel);
        passwordField = new JPasswordField();
        add(passwordField);

        loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (adminManager.login(username, password)) {
                    JOptionPane.showMessageDialog(AdminPanel.this, "登录成功！");
                } else {
                    JOptionPane.showMessageDialog(AdminPanel.this, "登录失败！");
                }
            }
        });
        add(loginButton);

        displayAllMembersButton = new JButton("查找所有会员信息");
        displayAllMembersButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                memberManager.displayAllMembers();
            }
        });
        add(displayAllMembersButton);

        displayMemberInfoButton = new JButton("查找会员");
        displayMemberInfoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = JOptionPane.showInputDialog(AdminPanel.this, "请输入会员用户名：");
                memberManager.displayMemberInfo(username);
            }
        });
        add(displayMemberInfoButton);

        displayMemberLogoutRecordsButton = new JButton("查看会员注销记录");
        displayMemberLogoutRecordsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = JOptionPane.showInputDialog(AdminPanel.this, "请输入会员用户名：");
                memberManager.displayMemberLogoutRecords(username);
            }
        });
        add(displayMemberLogoutRecordsButton);

        addMemberButton = new JButton("添加会员");
        addMemberButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] fields = {"用户名：", "密码：", "姓名：", "电话：", "会费："};
                JTextField[] textFields = new JTextField[fields.length];
                JPanel panel = new JPanel(new GridLayout(fields.length, 2));
                for (int i = 0; i < fields.length; i++) {
                    panel.add(new JLabel(fields[i]));
                    textFields[i] = new JTextField();
                    panel.add(textFields[i]);
                }
                int result = JOptionPane.showConfirmDialog(AdminPanel.this, panel, "添加会员", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (result == JOptionPane.OK_OPTION) {
                    String username = textFields[0].getText();
                    String password = textFields[1].getText();
                    String name = textFields[2].getText();
                    String phone = textFields[3].getText();
                    double fee = Double.parseDouble(textFields[4].getText());
                    if (memberManager.addMember(username, password, name, phone, fee)) {
                        JOptionPane.showMessageDialog(AdminPanel.this, "添加成功！");
                    } else {
                        JOptionPane.showMessageDialog(AdminPanel.this, "添加失败！");
                    }
                }
            }
        });
        add(addMemberButton);

        deleteMemberButton = new JButton("删除会员");
        deleteMemberButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = JOptionPane.showInputDialog(AdminPanel.this, "请输入会员用户名：");
                if (memberManager.deleteMember(username)) {
                    JOptionPane.showMessageDialog(AdminPanel.this, "删除成功！");
                } else {
                    JOptionPane.showMessageDialog(AdminPanel.this, "删除失败！");
                }
            }
        });
        add(deleteMemberButton);
    }
}
